package com.ruoyi.assest.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.assest.domain.AssetGzyusuanZi;
import com.ruoyi.assest.mapper.AssetGzyusuanZhuMapper;
import com.ruoyi.assest.domain.AssetGzyusuanZhu;
import com.ruoyi.assest.service.IAssetGzyusuanZhuService;

/**
 * 购置预算Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-02
 */
@Service
public class AssetGzyusuanZhuServiceImpl implements IAssetGzyusuanZhuService
{
    @Autowired
    private AssetGzyusuanZhuMapper assetGzyusuanZhuMapper;

    /**
     * 查询购置预算
     *
     * @param id 购置预算主键
     * @return 购置预算
     */
    @Override
    public AssetGzyusuanZhu selectAssetGzyusuanZhuById(Long id)
    {
        return assetGzyusuanZhuMapper.selectAssetGzyusuanZhuById(id);
    }

    /**
     * 查询购置预算列表
     *
     * @param assetGzyusuanZhu 购置预算
     * @return 购置预算
     */
    @Override
    public List<AssetGzyusuanZhu> selectAssetGzyusuanZhuList(AssetGzyusuanZhu assetGzyusuanZhu)
    {
        return assetGzyusuanZhuMapper.selectAssetGzyusuanZhuList(assetGzyusuanZhu);
    }

    /**
     * 新增购置预算
     *
     * @param assetGzyusuanZhu 购置预算
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAssetGzyusuanZhu(AssetGzyusuanZhu assetGzyusuanZhu)
    {
        int rows = assetGzyusuanZhuMapper.insertAssetGzyusuanZhu(assetGzyusuanZhu);
        insertAssetGzyusuanZi(assetGzyusuanZhu);
        return rows;
    }

    /**
     * 修改购置预算
     *
     * @param assetGzyusuanZhu 购置预算
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAssetGzyusuanZhu(AssetGzyusuanZhu assetGzyusuanZhu)
    {
        assetGzyusuanZhuMapper.deleteAssetGzyusuanZiByOneid(assetGzyusuanZhu.getId());
        insertAssetGzyusuanZi(assetGzyusuanZhu);
        return assetGzyusuanZhuMapper.updateAssetGzyusuanZhu(assetGzyusuanZhu);
    }

    /**
     * 批量删除购置预算
     *
     * @param ids 需要删除的购置预算主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAssetGzyusuanZhuByIds(Long[] ids)
    {
        assetGzyusuanZhuMapper.deleteAssetGzyusuanZiByOneids(ids);
        return assetGzyusuanZhuMapper.deleteAssetGzyusuanZhuByIds(ids);
    }

    /**
     * 删除购置预算信息
     *
     * @param id 购置预算主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAssetGzyusuanZhuById(Long id)
    {
        assetGzyusuanZhuMapper.deleteAssetGzyusuanZiByOneid(id);
        return assetGzyusuanZhuMapper.deleteAssetGzyusuanZhuById(id);
    }

    /**
     * 新增购置预算明细信息
     *
     * @param assetGzyusuanZhu 购置预算对象
     */
    public void insertAssetGzyusuanZi(AssetGzyusuanZhu assetGzyusuanZhu)
    {
        List<AssetGzyusuanZi> assetGzyusuanZiList = assetGzyusuanZhu.getAssetGzyusuanZiList();
        Long id = assetGzyusuanZhu.getId();
        if (StringUtils.isNotNull(assetGzyusuanZiList))
        {
            List<AssetGzyusuanZi> list = new ArrayList<AssetGzyusuanZi>();
            for (AssetGzyusuanZi assetGzyusuanZi : assetGzyusuanZiList)
            {
                assetGzyusuanZi.setOneid(String.valueOf(id));
                list.add(assetGzyusuanZi);
            }
            if (list.size() > 0)
            {
                assetGzyusuanZhuMapper.batchAssetGzyusuanZi(list);
            }
        }
    }
}
